package com.wzapp.sitposture.eshop.endpoint;

import com.wzapp.sitposture.common.restful.RestfulResult;
import com.wzapp.sitposture.eshop.entity.OrderPay;
import com.wzapp.sitposture.eshop.repository.OrderPayRepository;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能: 前端支付跳转<br/>
 *
 * @author yuxiaguang
 * @create 2018-06-20 16:44
 * @return
 **/
@Slf4j
@RestController
@RequestMapping("/eShop/orderPays")
public class OrderPayEndpoint {

    @Autowired
    OrderPayRepository orderPayRepository;


    @Autowired
    Environment environment;

    @GetMapping("")
    public ResponseEntity payResult(){

        log.info("--------------payResult------------" );
        String domain = environment.getProperty("ql.domain.web");
        // wap端定义的支付跳转地址 /pay-result
        return RestfulResult.ok("<!DOCTYPEhtml><htmllang=\"en\"><head><metacharset=\"UTF-8\"><metaname=\"viewport\"content=\"width=device-width, initial-scale=1.0\"><metahttp-equiv=\"X-UA-Compatible\"content=\"ie=edge\"><title></title></head><body><script>window.android&&window.android.close();setTimeout(function () {window.returnBack && window.returnBack()}, 3000);window.location.href = '"+ domain +"/pay-result';</script></body></html>");
    }

    @GetMapping("/search/findByOrderCode")
    public ResponseEntity findByOrderCode(@RequestParam("code") String code){

        log.info("--------------findByOrderCode------------" + code);
        OrderPay orderPay = orderPayRepository.findByOrderCode(code);
        log.info("------orderPay:" + orderPay);

        return RestfulResult.ok(orderPay);
    }
}
